package com.ygxsk.carhome.views.picker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Autuhor:itchen
 * Time:2020/1/9 0009  14:02
 * Description: this is 周历的工具类
 */
public class WeekUtils {


    /**
     * 获取当前的年的第一天和最后一天
     */
    public static String getYearCur() {

        Calendar calendarMonth = Calendar.getInstance();
        calendarMonth.clear();
        calendarMonth.setTimeInMillis(System.currentTimeMillis());

        int curYear = calendarMonth.get(Calendar.YEAR);//获得当前的年

        return getBetweenOfYear(curYear, 1, 12);
    }

    /**
     * 获取当前日期所在的 年+月
     */
    public static String getMonthCur() {

        Calendar calendarMonth = Calendar.getInstance();
        calendarMonth.clear();
        calendarMonth.setTimeInMillis(System.currentTimeMillis());

        int curYear = calendarMonth.get(Calendar.YEAR);//获得当前的年
        int curMonth = calendarMonth.get(Calendar.MONTH) + 1;//获取当前时间所在的年

        return curYear + "." + String.format("%02d", curMonth);

    }

    //单独的仅仅是获取月份
    public static String getMonthSingle() {

        Calendar calendarMonth = Calendar.getInstance();
        calendarMonth.clear();
        calendarMonth.setTimeInMillis(System.currentTimeMillis());

        //int curYear = calendarMonth.get(Calendar.YEAR);//获得当前的年
        int curMonth = calendarMonth.get(Calendar.MONTH) + 1;

        //return curYear + "." + String.format("%02d", curMonth);
        return String.format("%02d", curMonth);//01,02,03,04
        //return String.format(curMonth);

    }

    //获取当前月份的上一个月，
    public static String getMonthCurrentPrevious() {

        Calendar calendarMonth = Calendar.getInstance();
        calendarMonth.clear();
        calendarMonth.setTimeInMillis(System.currentTimeMillis());

        //int curYear = calendarMonth.get(Calendar.YEAR);//获得当前的年
        //获取当前的月份
        //int curMonth = calendarMonth.get(Calendar.MONTH) + 1;
        //设置为上一个月
        calendarMonth.add(Calendar.MONTH, -1);

        int resultMonth = calendarMonth.get(Calendar.MONTH) + 1;

        return String.format("%02d", resultMonth);//01,02,03,04
    }

    //获取当前月份的简单格式：数字1、2、3、4 等格式
    public static int getMonthSimpleInteger() {

        Calendar calendarMonth = Calendar.getInstance();
        calendarMonth.clear();
        calendarMonth.setTimeInMillis(System.currentTimeMillis());

        //int curYear = calendarMonth.get(Calendar.YEAR);//获得当前的年
        int curMonth = calendarMonth.get(Calendar.MONTH) + 1;//获得当前月份

        //return curYear + "." + String.format("%02d", curMonth);
        //return String.format("%02d", curMonth);//01,02,03,04

        return curMonth;
    }

    /**
     * 获取系统当前的年份--单独的获取年份；
     */
    public static String getYearSingle() {

        Calendar calendarYear = Calendar.getInstance();
        calendarYear.clear();
        calendarYear.setTimeInMillis(System.currentTimeMillis());

        int curYear = calendarYear.get(Calendar.YEAR);//获得当前的年

        return String.format("%02d", curYear);
    }

    //单纯的获取单独的 String 类型的年；这种是特殊计算过的，用于排名
    public static String getYearStringMy(int monthCurrent) {

        //默认其实也是当前的年份
        int resultYear = 0;

        try {

            //do
            Calendar calendarYear = Calendar.getInstance();
            calendarYear.clear();
            calendarYear.setTimeInMillis(System.currentTimeMillis());

            int curYear = calendarYear.get(Calendar.YEAR);//获得当前的年
            //默认是等于当前的年份；
            resultYear = curYear;

            if (monthCurrent == 1) {//如果当前月份是1月，那么初始的年份，要减掉1年；
                resultYear = curYear - 1;
            } else {
                resultYear = curYear;
            }

        } catch (Exception e) {
            //catch
            e.printStackTrace();
        }

        return String.format("%02d", resultYear);
    }

    //单纯的获取单独的 integer类型的年；这种是特殊计算过的，用于排名
    public static int getYearIntegerMy(int monthCurrent) {

        Calendar calendarYear = Calendar.getInstance();
        calendarYear.clear();
        calendarYear.setTimeInMillis(System.currentTimeMillis());

        int curYear = calendarYear.get(Calendar.YEAR);//获得当前的年

        int resultYear = curYear;
        if (monthCurrent == 1) {//如果当前月份是1月，那么初始的年份，要减掉1年
            resultYear = curYear - 1;
        }

        return curYear;
    }

    //int类型的年份，为了展示年份的弹窗赋值int相互适应
    public static int getYearSingleForYearDialog() {
        Calendar calendarYear = Calendar.getInstance();
        calendarYear.clear();
        calendarYear.setTimeInMillis(System.currentTimeMillis());
        int curYear = calendarYear.get(Calendar.YEAR);//获得当前的年
        //return String.format("%02d", curYear);
        return curYear;
    }

    /**
     * 获取当前系统 时间 所处于是哪一月
     */
    public static int getCurMonthInteger() {

        int curMonth = 0;

        try {
            //do

            Calendar calendarMonth = Calendar.getInstance();
            calendarMonth.clear();
            calendarMonth.setTimeInMillis(System.currentTimeMillis());

            curMonth = calendarMonth.get(Calendar.MONTH) + 1;//获取当前时间所在的年

        } catch (Exception e) {
            //catch
            e.printStackTrace();
        }

        return curMonth;
    }

    /**
     * 获取当前所在周 第几周
     */
    public static int getCurWeek() {

        Calendar calendarCurWeek = Calendar.getInstance();
        calendarCurWeek.clear();
        calendarCurWeek.setTimeInMillis(System.currentTimeMillis());

        int weekOfYear = calendarCurWeek.get(Calendar.WEEK_OF_YEAR);//获得当前日期属于今年的第几周

        return weekOfYear;
    }

    /**
     * 获取当年当周的时间间隔内容 ，当周的第一天和当周的最后一天
     */
    public static String getWeekCurBetween() {

        Calendar calendarWeek = Calendar.getInstance();
        calendarWeek.clear();
        calendarWeek.setTimeInMillis(System.currentTimeMillis());

        int weekYear = calendarWeek.get(Calendar.YEAR);//获得当前的年
        int weekOfYear = calendarWeek.get(Calendar.WEEK_OF_YEAR);//获得当前日期属于今年的第几周

        return getBetweenOfWeekFormat(weekYear, weekOfYear);

    }

    /**
     * 获取当前时间的时间格式 就是获取当年的年月
     */
    public static String getFormat(Date date) {

        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
            return df.format(date);
        }

        return "";
    }

    /**
     * 修改为服务器端的关于 斜杠的日期格式
     */
    public static String getFormatPass(Date date) {

        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            return df.format(date);
        }

        return "";
    }

    /**
     * 获取一个固定格式的 当周的第一天和最后一天间隔
     */
    public static String getBetweenOfWeek(int year, int weekindex) {

        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");

        Calendar calendar = Calendar.getInstance();
        calendar.clear();

        calendar.set(Calendar.YEAR, year);// 设置年  动态的
        calendar.set(Calendar.WEEK_OF_YEAR, weekindex);// 设置周  动态的

        // 这两个设置必须先做，然后可以观察后边的变化
        calendar.setFirstDayOfWeek(Calendar.MONDAY);// 设置一周的第一天是 周一
        calendar.setMinimalDaysInFirstWeek(2);// 设置一周最少2天


        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 设置周几 也就是这周的第一天是 周几
        //System.out.println("周一：" + format.format(c.getTime())); // 获取日期
        String beginTime = sdf.format(calendar.getTime());
        //LogUtils.d("itchen--beginTime-"+beginTime);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);// 设置周几 也就是这周的末尾一天是周日
        //System.out.println("周日：" + format.format(c.getTime())); // 获取日期
        String endTime = sdf.format(calendar.getTime());
        //LogUtils.d("itchen--endTime-"+endTime);

        String timeBetween = "(" + beginTime + " - " + endTime + ")";

        //LogUtils.d("itchen--->"+year+"--的-第--"+weekindex+"--周："+timeBetween);

        return timeBetween;

    }

    //----------------------------------------------------------------------------------------------

    //第一种 哪一年 哪一周的时间格式获取

    /**
     * 获取一个格式的当周的第一天和最后一天的时间 形式
     */
    public static String getBetweenOfWeekFormat(int year, int weekindex) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");

        Calendar calendar = Calendar.getInstance();
        calendar.clear();

        calendar.set(Calendar.YEAR, year);// 设置年  动态的
        calendar.set(Calendar.WEEK_OF_YEAR, weekindex);// 设置周  动态的

        // 这两个设置必须先做，然后可以观察后边的变化
        calendar.setFirstDayOfWeek(Calendar.MONDAY);// 设置一周的第一天是 周一
        calendar.setMinimalDaysInFirstWeek(2);// 设置一周最少2天


        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 设置周几 也就是这周的第一天是 周几
        //System.out.println("周一：" + format.format(c.getTime())); // 获取日期
        String beginTime = sdf.format(calendar.getTime());
        //LogUtils.d("itchen--beginTime-"+beginTime);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);// 设置周几 也就是这周的末尾一天是周日

        //System.out.println("周日：" + format.format(c.getTime())); // 获取日期
        String endTime = sdf.format(calendar.getTime());
        //LogUtils.d("itchen--endTime-"+endTime);

        String timeBetween = beginTime + " - " + endTime;

        //LogUtils.d("itchen--->"+year+"--的-第--"+weekindex+"--周："+timeBetween);

        return timeBetween;

    }

    //第二种  哪一年 哪一周的时间格式获取:为了我使用往服务器传递时间的格式化
    public static String getBetweenOfWeekFormatPass(int year, int weekindex) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        Calendar calendar = Calendar.getInstance();
        calendar.clear();

        calendar.set(Calendar.YEAR, year);// 设置年  动态的
        calendar.set(Calendar.WEEK_OF_YEAR, weekindex);// 设置周  动态的

        // 这两个设置必须先做，然后可以观察后边的变化
        calendar.setFirstDayOfWeek(Calendar.MONDAY);// 设置一周的第一天是 周一
        calendar.setMinimalDaysInFirstWeek(2);// 设置一周最少2天


        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 设置周几 也就是这周的第一天是 周几
        //System.out.println("周一：" + format.format(c.getTime())); // 获取日期
        String beginTime = sdf.format(calendar.getTime());
        //LogUtils.d("itchen--beginTime-"+beginTime);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);// 设置周几 也就是这周的末尾一天是周日

        //System.out.println("周日：" + format.format(c.getTime())); // 获取日期
        String endTime = sdf.format(calendar.getTime());
        //LogUtils.d("itchen--endTime-"+endTime);

        String timeBetween = beginTime + "#" + endTime;

        //LogUtils.d("itchen--->"+year+"--的-第--"+weekindex+"--周："+timeBetween);

        return timeBetween;

    }

    //----------------------------------------------------------------------------------------------

    /**
     * 获取指定年月的第一天
     */
    public static String getFirstDayOfMonth(int year, int month) {

        Calendar cal = Calendar.getInstance();
        cal.clear();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");

        return sdf.format(cal.getTime());

    }

    /**
     * 获取一定格式的年月日的时间戳内容
     */
    public static String getFirstDayOfMonthFormat(int year, int month) {

        Calendar cal = Calendar.getInstance();
        cal.clear();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        return sdf.format(cal.getTime());

    }

    /**
     * 获取指定年月的最后一天
     */
    public static String getLastDayOfMonth(int year, int month) {

        Calendar cal = Calendar.getInstance();
        cal.clear();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");

        return sdf.format(cal.getTime());
    }

    /**
     * 获取指定年月的最后一天  获取指定年月日的最后一天的斜杠格式的
     */
    public static String getLastDayOfMonthFormat(int year, int month) {

        Calendar cal = Calendar.getInstance();
        cal.clear();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        return sdf.format(cal.getTime());
    }

    /**
     * 获取一年的第一天和最后一天的日期总的间隔
     */
    public static String getBetweenOfYear(int year, int monthFirst, int monthLast) {
        return getFirstDayOfMonth(year, monthFirst) + " - " + getLastDayOfMonth(year, monthLast);
    }


    //日历如何获取  当前年的每一个月份 的 第一天和最后一天
    /***/
    /*public static String getFirstDayOfMonth(){
    }*/

    /**获取本周的开始时间*/
    /*public static Date getBeginDayOfWeek(){

        Date date = new Date();

        if (date == null) {

            return null;

        }

        Calendar cal = Calendar.getInstance();
        cal.clear();

        cal.setTime(date);

        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);

        if (dayofweek == 1) {
            dayofweek += 7;
        }

        cal.add(Calendar.DATE, 2 - dayofweek);

        return getDayStartTime(cal.getTime());

    }*/


    /**获取本周的结束时间*/
   /* public static Date getEndDayOfWeek(){

        Calendar cal = Calendar.getInstance();

        cal.setTime(getBeginDayOfWeek());

        cal.add(Calendar.DAY_OF_WEEK, 6);

        Date weekEndSta = cal.getTime();

        return getDayEndTime(weekEndSta);

    }*/

    //获取某个日期的开始时间

    /*public static Timestamp getDayStartTime(Date d) {

        Calendar calendar = Calendar.getInstance();

        if(null != d) calendar.setTime(d);

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.MILLISECOND, 0);

        return new Timestamp(calendar.getTimeInMillis());

    }*/


}
